package com.rerun.service.impl;

import com.rerun.common.bean.Rest;
import com.rerun.common.email.HtmlMailTemplate;
import com.rerun.common.email.MailUtil;
import com.rerun.common.email.bean.Email;
import com.rerun.common.util.Validator;
import com.rerun.entity.vo.InterviewTemplateParam;
import com.rerun.entity.vo.ResumeTemplateParam;
import com.rerun.entity.EmailHistory;
import com.rerun.entity.Job;
import com.rerun.entity.MemberUser;
import com.rerun.entity.YoyoPark;
import com.rerun.enums.EmailTypeEnum;
import com.rerun.service.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.Objects;

/**
 * The type Mail serivce.
 *
 * @author dax.
 * @version v1.0
 * @since 2017 /10/12 17:00
 */
@Service
public class MailServiceImpl implements IMailService {

    private static final String APP_NAME = "【优教招聘】";
    @Resource
    private MailUtil mailUtil;
    @Resource
    private IMemberUserService iMemberUserService;
    @Resource
    private IJobService iJobService;
    @Resource
    private IResumeService iResumeService;
    @Resource
    private IEmailHistoryService iEmailHistoryService;

    @Override
    public Rest sendResumeEmail(HttpServletRequest request, String memberId, String jobId) throws IOException, MessagingException {
        ResumeTemplateParam param = iResumeService.selectResumeDetailByMemberId(memberId);
        Job job = iJobService.findAvailableJobById(jobId);
        if (Objects.nonNull(param) && Objects.nonNull(job)) {
            YoyoPark park = job.getYoyoPark();
            if (Objects.nonNull(park)) {
                String emailAddress = park.getEmail();

                if (Validator.isEmail(emailAddress)) {
                    String jobName = job.getJobName();
                    param.setJobName(jobName);
                    Email email = new Email();
                    email.setToAddress(emailAddress);
                    StringBuffer subject = new StringBuffer(param.getName());
                    subject.append("-").append("应聘").append(jobName).append("-期望月薪").append(param.getHopePay()).append("-工作经验").append(param.getExperience()).append(APP_NAME);
                    email.setSubject(subject.toString());
                    email.setContent(HtmlMailTemplate.receiveResumeTemplate(param));
                    mailUtil.sendMail(email, request);

                    recordHistory(emailAddress, EmailTypeEnum.DELIVER);

                    return Rest.ok();
                }
            }
        }
        return Rest.failure("信息不完整");
    }

    @Override
    public Rest interviewInvitationEmail(HttpServletRequest request, String jobId, String memberId, String interviewTime, String remark) throws IOException, MessagingException {
        MemberUser memberUser = iMemberUserService.selectById(memberId);
        Job job = iJobService.findAvailableJobById(jobId);
        if (Objects.nonNull(memberUser) && Objects.nonNull(job)) {
            String emailAddress = memberUser.getEmail();
            if (Validator.isEmail(emailAddress)) {
                long sex = memberUser.getSex();
                String name = memberUser.getNickname();
                String receiverName = name == null ? sex == 1 ? "先生" : "女士" : name;
                String jobName = job.getJobName();

                YoyoPark park = job.getYoyoPark();
                String parkName = park.getParkName();
                String address = park.getAddress();
                String phone = park.getPhone();
                String hrName = park.getPresident();

                InterviewTemplateParam param = new InterviewTemplateParam();
                param.setReceiverName(receiverName);
                param.setParkName(parkName);
                param.setJobName(jobName);
                param.setInterviewTime(interviewTime);
                param.setHrName(hrName);
                param.setAddress(address);
                param.setPhone(phone);
                param.setRemark(remark);

                Email email = new Email();
                email.setToAddress(emailAddress);
                email.setSubject(parkName + " 邀请您面试  " + APP_NAME);
                email.setContent(HtmlMailTemplate.interviewTemplate(param));
                mailUtil.sendMail(email, request);

                recordHistory(emailAddress, EmailTypeEnum.INVITATION);

                return Rest.ok();
            }
        }
        return Rest.failure("信息不完整");
    }

    @Override
    public Rest refuseEmail(String parkName, String emailAddress) throws IOException, MessagingException {
        Email email = new Email();
        email.setSubject(parkName + "  简历反馈：不合适  " + APP_NAME);
        email.setContent("很遗憾 您的简历提供的信息不符合我们的职位要求，祝您好运。  ————" + parkName);
        email.setToAddress(emailAddress);
        mailUtil.sendSimpleMail(email);

        recordHistory(emailAddress, EmailTypeEnum.REFUSE);

        return Rest.ok();
    }

    /**
     * 记录发送历史
     *
     * @param emailAddress  收件人
     * @param emailTypeEnum 类型枚举
     */
    private void recordHistory(String emailAddress, EmailTypeEnum emailTypeEnum) {
        EmailHistory history = new EmailHistory();
        history.setToAddress(emailAddress);
        history.setCreateTime(new Date());
        history.setEmailType(emailTypeEnum.getKey());
        iEmailHistoryService.insert(history);
    }
}
